﻿Imports System
Imports System.Text
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports FrameworkTeste
Imports Spring.Objects.Factory.Xml
Imports Spring.Transaction.Interceptor
Imports Spring.Transaction
Imports LS.Leilao

<TestClass()> Public Class LeilaoTeste
    Inherits TesteTransacionalInjecaoDependenciaSpring

#Region " ConfigLocations "

    Protected Overrides ReadOnly Property ConfigLocations() As String()
        Get
            NamespaceParserRegistry.RegisterParser(GetType(Spring.Data.Config.DatabaseNamespaceParser))
            Return New [String]() {"assembly://Teste.Leilao.Negoc/Teste.Leilao.Negoc/AutoWired.xml"}
        End Get
    End Property

#End Region

    Private testContextInstance As TestContext
    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "Additional test attributes"
    '
    ' You can use the following additional attributes as you write your tests:
    '
    ' Use ClassInitialize to run code before running the first test in the class
    ' <ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    ' End Sub
    '
    ' Use ClassCleanup to run code after all tests in a class have run
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' Use TestInitialize to run code before running each test
    <TestInitialize()> Public Overloads Sub Initialize()
        MyBase.DefaultRollback = False
        MyBase.Initialize()
    End Sub
    '
    ' Use TestCleanup to run code after each test has run
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

#Region "Particoes de Equivalencia"

    'EXEMPLO: Public Const PARTICAO_XXX as String = " Select CAMPO_X from TABELA_Y "

#End Region

#Region "Classes Injetadas"

    Private _particaoEquivalencia As ParticaoEquivalencia
    Public Property ParticaoEquivalencia() As ParticaoEquivalencia
        Get
            Throw New NotImplementedException("Nao esqueca de implementar o codigo abaixo")
            '_particaoEquivalencia = New ParticaoEquivalencia("UMA_CONNECTION_NAME")
            Return _particaoEquivalencia
        End Get
        Set(ByVal value As ParticaoEquivalencia)
            _particaoEquivalencia = value
        End Set
    End Property


    Private _usuarioDao As IUsuarioDao
    Public Property UsuarioDao() As IUsuarioDao
        Get
            Return _usuarioDao
        End Get
        Set(ByVal value As IUsuarioDao)
            _usuarioDao = value
        End Set
    End Property


    Private _pessoaDao As IPessoaDao
    Public Property PessoaDao() As IPessoaDao
        Get
            Return _pessoaDao
        End Get
        Set(ByVal value As IPessoaDao)
            _pessoaDao = value
        End Set
    End Property

    Private _pecaDao As IPecaDao
    Public Property PecaDao() As IPecaDao
        Get
            Return _pecaDao
        End Get
        Set(ByVal value As IPecaDao)
            _pecaDao = value
        End Set
    End Property


    Private _leilaoDao As ILeilaoDao
    Public Property LeilaoDao() As ILeilaoDao
        Get
            Return _leilaoDao
        End Get
        Set(ByVal value As ILeilaoDao)
            _leilaoDao = value
        End Set
    End Property

    Private _loteDao As ILoteDao
    Public Property LoteDao() As ILoteDao
        Get
            Return _loteDao
        End Get
        Set(ByVal value As ILoteDao)
            _loteDao = value
        End Set
    End Property


    Private _regraDao As IRegraDao
    Public Property RegraDao() As IRegraDao
        Get
            Return _regraDao
        End Get
        Set(ByVal value As IRegraDao)
            _regraDao = value
        End Set
    End Property

    Private _comissaoDao As IComissaoDao
    Public Property ComissaoDao() As IComissaoDao
        Get
            Return _comissaoDao
        End Get
        Set(ByVal value As IComissaoDao)
            _comissaoDao = value
        End Set
    End Property


    Private _lanceDao As ILanceDao
    Public Property LanceDao() As ILanceDao
        Get
            Return _lanceDao
        End Get
        Set(ByVal value As ILanceDao)
            _lanceDao = value
        End Set
    End Property


    Private _contatoDao As IContatoDao
    Public Property ContatoDao() As IContatoDao
        Get
            Return _contatoDao
        End Get
        Set(ByVal value As IContatoDao)
            _contatoDao = value
        End Set
    End Property


    Private _vendaDao As IVendaDao
    Public Property VendaDao() As IVendaDao
        Get
            Return _vendaDao
        End Get
        Set(ByVal value As IVendaDao)
            _vendaDao = value
        End Set
    End Property

    Private _faturaDao As IFaturaDao
    Public Property FaturaDao() As IFaturaDao
        Get
            Return _faturaDao
        End Get
        Set(ByVal value As IFaturaDao)
            _faturaDao = value
        End Set
    End Property


    Private _imagemPecaDao As IImagemPecaDao
    Public Property ImagemPecaDao() As IImagemPecaDao
        Get
            Return _imagemPecaDao
        End Get
        Set(ByVal value As IImagemPecaDao)
            _imagemPecaDao = value
        End Set
    End Property


#End Region

    <TestMethod()> Public Sub Start()
        Dim _usuario01, _usuario02, _usuario03, _usuario04, _usuario05 As Usuario
        Dim _pessoa01, _pessoa02, _pessoa03, _pessoa04, _pessoa05 As Pessoa
        Dim _peca01, _peca02, _peca03, _peca04, _peca05, _peca06, _peca07, _peca08 As Peca
        Dim _imagem01, _imagem02 As ImagemPeca
        Dim _lote01, _lote02 As Lote
        Dim _leilao01 As Dominio.Leilao
        Dim _regraLeilao01 As RegraLeilao
        Dim _regraLote01 As RegraLote
        Dim _comissao01 As Comissao
        Dim _lance01, _lance02, _lance03, _lance04, _lance05, _lance06, _lance07 As Lance
        Dim _contato01, _contato02, _contato03 As Contato
        Dim _venda01 As Venda
        Dim _fatura01 As Fatura

        _pessoa01 = New Pessoa With {.Nome = "Leonardo Santana", .Cidade = "Rio de Janeiro", .Uf = "RJ", .DocumentoIdentificacao = "123", .Email = "email@gmail.com"}
        _pessoa02 = New Pessoa With {.Nome = "Ana Tereza", .Cidade = "Rio de Janeiro", .Uf = "RJ", .DocumentoIdentificacao = "245", .Email = "ana@gmail.com"}
        _pessoa03 = New Pessoa With {.Nome = "Marco", .Cidade = "Rio de Janeiro", .Uf = "RJ", .DocumentoIdentificacao = "678", .Email = "marco@gmail.com"}
        _pessoa04 = New Pessoa With {.Nome = "Antonio", .Cidade = "Rio de Janeiro", .Uf = "RJ", .DocumentoIdentificacao = "908", .Email = "antonio@gmail.com"}
        _pessoa05 = New Pessoa With {.Nome = "Carlos", .Cidade = "São Paulo", .Uf = "SP", .DocumentoIdentificacao = "459", .Email = "carlos@gmail.com"}

        _pessoa01 = Me.SalvarPessoa(_pessoa01)
        _pessoa02 = Me.SalvarPessoa(_pessoa02)
        _pessoa03 = Me.SalvarPessoa(_pessoa03)
        _pessoa04 = Me.SalvarPessoa(_pessoa04)
        _pessoa05 = Me.SalvarPessoa(_pessoa05)

        _usuario01 = New Usuario With {.Login = "LSANTANA", .Pessoa = _pessoa01, .Habilitado = False}
        _usuario01 = Me.SalvarUsuario(_usuario01)

        _contato01 = New Telefone With {.DDD = "21", .Numero = "2538-9967", .Pessoa = _pessoa01, .Usuario = _usuario01}
        _contato02 = New Telefone With {.DDD = "21", .Numero = "6789-9908", .Pessoa = _pessoa02, .Usuario = _usuario01}
        _contato03 = New Email With {.Endereco = "teste@test.com", .Pessoa = _pessoa01, .Usuario = _usuario01}

        Me.ContatoDao.Salvar(_contato01)
        Me.ContatoDao.Salvar(_contato02)
        Me.ContatoDao.Salvar(_contato03)


        _usuario02 = New Usuario With {.Login = "ANA", .Pessoa = _pessoa02, .Habilitado = True}
        _usuario03 = New Usuario With {.Login = "MARCO", .Pessoa = _pessoa03, .Habilitado = True}
        _usuario04 = New Usuario With {.Login = "ANTONIO", .Pessoa = _pessoa04, .Habilitado = True}
        _usuario05 = New Usuario With {.Login = "CARLOS", .Pessoa = _pessoa05, .Habilitado = True}


        _usuario02 = Me.SalvarUsuario(_usuario02)
        _usuario03 = Me.SalvarUsuario(_usuario03)
        _usuario04 = Me.SalvarUsuario(_usuario04)
        _usuario05 = Me.SalvarUsuario(_usuario04)

        _peca01 = New Peca With {.Descricao = "castiçal de ouro", .Proprietario = _usuario01}
        _peca02 = New Peca With {.Descricao = "Moeda 1800", .Proprietario = _usuario01}
        _peca03 = New Peca With {.Descricao = "Selo Chinês", .Proprietario = _usuario02}
        _peca04 = New Peca With {.Descricao = "Moeda 1500", .Proprietario = _usuario02}
        _peca05 = New Peca With {.Descricao = "Candelabro folheado", .Proprietario = _usuario02}
        _peca06 = New Peca With {.Descricao = "Livro 1600", .Proprietario = _usuario02}
        _peca07 = New Peca With {.Descricao = "Moeda prata", .Proprietario = _usuario02}
        _peca08 = New Peca With {.Descricao = "500 fotografias Rio antigo", .Proprietario = _usuario02}

        Me.PecaDao.Salvar(_peca01)
        Me.PecaDao.Salvar(_peca02)
        Me.PecaDao.Salvar(_peca03)
        Me.PecaDao.Salvar(_peca04)
        Me.PecaDao.Salvar(_peca05)
        Me.PecaDao.Salvar(_peca06)
        Me.PecaDao.Salvar(_peca07)
        Me.PecaDao.Salvar(_peca08)


        _imagem01 = New ImagemPeca With {.Extensao = "jpg", .Peca = _peca01, .Tamanho = 100, .Descricao = "Peça de teste"}
        _imagem02 = New ImagemPeca With {.Extensao = "jpg", .Peca = _peca01, .Tamanho = 3500, .Descricao = "Peça de teste 02"}

        Me.ImagemPecaDao.Salvar(_imagem01)
        Me.ImagemPecaDao.Salvar(_imagem02)

        _comissao01 = New Comissao With {.TipoComissao = TipoComissao.ValorFixo, .Valor = 10}
        Me.ComissaoDao.Salvar(_comissao01)

        _regraLeilao01 = New RegraLeilao With {.ValorLimite = 20, .Autor = _peca01.Proprietario, .Comissao = _comissao01}
        Me.RegraDao.Salvar(_regraLeilao01)

        _leilao01 = New Dominio.Leilao With {.Leiloeiro = _usuario02, .Autor = _peca01.Proprietario, .Ativo = False, .Regra = _regraLeilao01}
        Me.LeilaoDao.Salvar(_leilao01)

        _regraLote01 = New RegraLote With {.Autor = _usuario01, .LanceMinimo = 150, .Comissao = _comissao01}
        Me.RegraDao.Salvar(_regraLote01)

        _lote01 = New Lote With {.Descricao = "Moedas de 1500, florença Itália", .Pecas = New List(Of Peca), .RegraLote = _regraLote01, .Leilao = _leilao01}
        _lote01.Pecas.Add(_peca01)
        Me.LoteDao.Salvar(_lote01)

        _lote01.Pecas.Add(_peca02)
        Me.LoteDao.Alterar(_lote01)

        _lote01.Leilao = _leilao01
        Me.LoteDao.Alterar(_lote01)


        _lote02 = New Lote With {.Descricao = "Peças antigas de geladeira", .Pecas = New List(Of Peca), .RegraLote = _regraLote01, .Leilao = _leilao01}
        _lote02.Pecas.Add(_peca05)
        _lote02.Pecas.Add(_peca06)
        _lote02.Pecas.Add(_peca07)
        _lote02.Pecas.Add(_peca08)
        Me.LoteDao.Salvar(_lote02)

        _lance01 = New Lance With {.Autor = _usuario03, .DataHora = Now, .Lote = _lote01, .Valor = 100}
        _lance02 = New Lance With {.Autor = _usuario04, .DataHora = Now.AddMinutes(4), .Lote = _lote01, .Valor = 150}
        _lance03 = New Lance With {.Autor = _usuario03, .DataHora = Now.AddMinutes(5), .Lote = _lote01, .Valor = 180}
        _lance04 = New Lance With {.Autor = _usuario04, .DataHora = Now.AddMinutes(8), .Lote = _lote01, .Valor = 200}

        _lance05 = New Lance With {.Autor = _usuario04, .DataHora = Now.AddMinutes(20), .Lote = _lote02, .Valor = 500}
        _lance06 = New Lance With {.Autor = _usuario05, .DataHora = Now.AddMinutes(23), .Lote = _lote02, .Valor = 600}
        _lance07 = New Lance With {.Autor = _usuario03, .DataHora = Now.AddMinutes(30), .Lote = _lote02, .Valor = 630}


        Me.LanceDao.Salvar(_lance01)
        Me.LanceDao.Salvar(_lance02)
        Me.LanceDao.Salvar(_lance03)
        Me.LanceDao.Salvar(_lance04)
        Me.LanceDao.Salvar(_lance05)
        Me.LanceDao.Salvar(_lance06)
        Me.LanceDao.Salvar(_lance07)


        _venda01 = New Venda With {.Comprador = _lance07.Autor, .LanceDeFechamento = _lance07, .Lote = _lance07.Lote, .TimeStamp = _lance07.DataHora.AddMinutes(60)}

        Me.VendaDao.Salvar(_venda01)


        _fatura01 = New Fatura With {.DataFaturamento = Now, .Vencimento = Now.AddDays(5), .Venda = _venda01, .Valor = 5, .UsuarioFaturado = _venda01.Lote.Leilao.Autor, .Status = StatusFatura.EmAberto}

        Me.FaturaDao.Salvar(_fatura01)


        Assert.IsTrue(True)
    End Sub


    <Transaction(TransactionPropagation.Required)> _
        Protected Function SalvarUsuario(ByVal _usuario As Usuario) As Usuario

        Me.UsuarioDao.Salvar(_usuario)

        Return _usuario

    End Function

    <Transaction(TransactionPropagation.Required)> _
        Protected Function SalvarPessoa(ByVal _pessoa As Pessoa) As Pessoa

        Me.PessoaDao.Salvar(_pessoa)

        Return _pessoa

    End Function

    '<TestMethod()> Public Sub Nome_Da_Operacao_Negativo()
    '    Assert.Inconclusive("Exemplo de teste negativo")
    'End Sub

End Class

